package com.globallogic.jbtask1;

import java.util.Scanner;
import java.lang.NumberFormatException;

public class PrimeNumbers {

    public int getTopNumber() {

        Scanner input = new Scanner(System.in);
        String str;               
        int value = -1;
         
        do {
 
            try {
 
                str = input.nextLine();

                value = Integer.parseInt(str);
                 

            } catch (NumberFormatException e) {

                System.out.println("Entered value is not a number. Try once more, please.");
 
            }

        } while (value == -1);

        return value;    
    } 

    public String getPrimeList(int value) {

        StringBuilder stringBuilder = new StringBuilder();

        for (int i = 1; i < value; i++) {

            boolean isPrime = true;
    
            for (int j = 2; j < i; j++) {

                if ((i % j) == 0) {
                    isPrime = false;
                    break;
                }

            }
            
            if (isPrime) {

                stringBuilder.append(i + " "); 

            }

        }
        
        return stringBuilder.toString();

    }

 
    public static void main(String[] args) {
        
        int topNumber;
        String primeList;
        PrimeNumbers primeNumbersApp = new PrimeNumbers();


        topNumber = primeNumbersApp.getTopNumber();

        System.out.println("Enter valid integer value, please... ");    
        System.out.println();

        primeList = primeNumbersApp.getPrimeList(topNumber);
    
        System.out.println("The prime numbers are: " + primeList);
        System.out.println();


    } 

}